package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.EmDeclaration;
import com.ruoyi.system.domain.EmRecord;
import com.ruoyi.system.enums.DeclarationStateEnum;
import com.ruoyi.system.mapper.EmDeclarationMapper;
import com.ruoyi.system.mapper.EmRecordMapper;
import com.ruoyi.system.service.IEmDeclarationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 灾害申报Service业务层处理
 *
 * @author yangcan
 * @date 2023-04-07
 */
@Service
public class EmDeclarationServiceImpl extends ServiceImpl<EmDeclarationMapper,EmDeclaration> implements IEmDeclarationService
{
    @Autowired
    private EmDeclarationMapper emDeclarationMapper;

    @Autowired
    private EmRecordMapper recordMapper ;

    /**
     * 查询灾害申报
     *
     * @param id 灾害申报主键
     * @return 灾害申报
     */
    @Override
    public EmDeclaration selectEmDeclarationById(Long id)
    {
        return emDeclarationMapper.selectById(id);
    }

    /**
     * 查询灾害申报列表
     *
     * @param emDeclaration 灾害申报
     * @return 灾害申报
     */
    @Override
    public List<EmDeclaration> selectEmDeclarationList(EmDeclaration emDeclaration)
    {
        return emDeclarationMapper.selectEmDeclarationList(emDeclaration);
    }

    /**
     * 新增灾害申报
     *
     * @param emDeclaration 灾害申报
     * @return 结果
     */
    @Override
    public int insertEmDeclaration(EmDeclaration emDeclaration)
    {
        emDeclaration.setCreateBy(SecurityUtils.getLoginUser().getUser().getNickName());
        emDeclaration.setCreateTime(DateUtils.getNowDate());
        return emDeclarationMapper.insertEmDeclaration(emDeclaration);
    }

    /**
     * 修改灾害申报
     *
     * @param emDeclaration 灾害申报
     * @return 结果
     */
    @Override
    public int updateEmDeclaration(EmDeclaration emDeclaration)
    {
        emDeclaration.setUpdateTime(DateUtils.getNowDate());
        return emDeclarationMapper.updateEmDeclaration(emDeclaration);
    }

    /**
     * 批量删除灾害申报
     *
     * @param ids 需要删除的灾害申报主键
     * @return 结果
     */
    @Override
    public int deleteEmDeclarationByIds(Long[] ids)
    {
        return emDeclarationMapper.deleteEmDeclarationByIds(ids);
    }

    /**
     * 删除灾害申报信息
     *
     * @param id 灾害申报主键
     * @return 结果
     */
    @Override
    public int deleteEmDeclarationById(Long id)
    {
        return emDeclarationMapper.deleteEmDeclarationById(id);
    }

    @Override
    public void acceptDeclaration(Long id) {
        EmDeclaration declaration = emDeclarationMapper.selectById(id);
        declaration.setState(DeclarationStateEnum.DECLARATION_ACCEPT.getCode());
        emDeclarationMapper.updateById(declaration) ;
        /**生成 预警档案*/
        EmRecord record = new EmRecord();
        record.setTitle(declaration.getTitle());
        record.setContext(declaration.getContent());
        record.setCreateBy(SecurityUtils.getLoginUser().getUser().getNickName());
        record.setCreateTime(new Date());
        record.setDeclarationId(id);
        record.setImage(declaration.getImage());
        recordMapper.insert(record) ;
    }
}
